package com.yst.gateway.monitor.services.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yst.gateway.monitor.common.utils.DateUtils;
import com.yst.gateway.monitor.common.utils.LoginUserCache;
import com.yst.gateway.monitor.common.utils.UUIDGenerator;
import com.yst.gateway.monitor.dao.QzChannelInterfaceMapper;
import com.yst.gateway.monitor.dao.QzChannelsMapper;
import com.yst.gateway.monitor.models.QzChannelInterface;
import com.yst.gateway.monitor.models.QzChannels;
import com.yst.gateway.monitor.services.QzChannelService;

@Service
@Scope("prototype")
public class QzChannelServiceImpl implements QzChannelService{

	private static final Logger logger = Logger.getLogger(QzChannelServiceImpl.class);
	
	@Autowired
	private QzChannelsMapper qzChannelsMapper;
	@Autowired
	private QzChannelInterfaceMapper qzchannelinter;
	
	@Override
	public PageInfo<QzChannels> getByPage(int pageNum, int pageSize,
			Map<String, Object> paramMap) {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<QzChannels>(qzChannelsMapper.selectAll(paramMap));
	}

	@Override
	public boolean add(QzChannels t) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> busmap = new HashMap<String, Object>();
		map.put("channelId", UUIDGenerator.getUUID());
		map.put("name", t.getName());
		map.put("code", t.getCode());
		map.put("memo", t.getMemo());
		map.put("status", 2);
		map.put("ctime", DateUtils.getNowTime());
		map.put("creator", LoginUserCache.getUserId());
		map.put("utime", DateUtils.getNowTime());
		map.put("modifier", LoginUserCache.getUserId());
		int result = qzChannelsMapper.insert(map);
		String str[] = t.getBusinessId().replace("[", "").replace("]", "").split(",");
		for(int i=0; i<str.length; i++) {
			busmap.put("cha_bus_id", UUIDGenerator.getUUID());
			busmap.put("channel_id", map.get("channelId"));
			busmap.put("business_id", str[i].trim());
			logger.info("传入参数:" + map.get("channel_id") +","+ map.get("business_id"));
			qzChannelsMapper.insertBatch(busmap);
		}
		if(result >= 1) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean update(QzChannels t) {
		QzChannels qz = new QzChannels();
//		if(t.getBusinessId() != "[]" && t.getBusinessId() != "{}") {
//			QzChannels resultqz = qzchannel.selectByPrimaryKey(t.getChannelId());
//			String resultstr[] = resultqz.getBusinessname().split(",");
//			String resbusId[] = resultqz.getBusinessId().split(",");
//			String paramstr[] = t.getBusinessId().replace("[", "").replace("]", "").split(",");
//			if(paramstr.length > resultstr.length) {
//				for(int i=0; i<paramstr.length; i++) {
//					Map<String, Object> busmap = new HashMap<String, Object>();
//					String[] paramname = paramstr[i].split(";");
//					int num = 0;
//					for(int j=0; j<resultstr.length; j++) {
//						if(resultstr[j].equals(paramname[1])) {
//							num = 0;
//							break;
//						}else {
//							num = 1;
//						}
//					}
//					if(num > 0) {
//						busmap.put("cha_bus_id", UUIDGenerator.getUUID());
//						busmap.put("channel_id", t.getChannelId());
//						busmap.put("business_id", paramname[0].trim());
//						qzchannel.insertBatch(busmap);
//					}
//				}
//			}else {
//				for(int i=0; i<resbusId.length; i++) {
//					Map<String, Object> busmap = new HashMap<String, Object>();
//					int num = 0;
//					for(int j=0; j<paramstr.length; j++) {
//						String[] paramname = paramstr[j].split(";");
//						if(resbusId[i].equals(paramname[0].trim())) {
//							num++;
//							break;
//						}else {
//							continue;
//						}
//					}
//					if(num == 0) {
//						System.out.println("删除结果:" + num);
//						busmap.put("channel_id", t.getChannelId());
//						busmap.put("business_id", resbusId[i].trim());
//						qzchannel.delByPrimaryKey(busmap);
//					}
//				}
//			}
//		}
		qz.setChannelId(t.getChannelId());
		qz.setName(t.getName());
		qz.setCode(t.getCode());
		qz.setMemo(t.getMemo());
		qz.setStatus(t.getStatus());
		qz.setUtime(DateUtils.getNowTime());
		qz.setModifier(LoginUserCache.getUserId());
		qz.setBusinessId(t.getBusinessId());
		 
		
		int result = qzChannelsMapper.updateByPrimaryKey(qz);
		if(result>0){
			//删除原来关联的业务数据
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("channelId", t.getChannelId());
			result = qzChannelsMapper.delChannelBusiById(map);
			//插入新的关联业务数据
			
			if(t.getBusinessId() != null && t.getBusinessId().length()>0){				
				String[] busiArry = t.getBusinessId().split(",");
				if(busiArry.length>0){
					for(int i=0; i<busiArry.length; i++) {
						map.put("cha_bus_id", UUIDGenerator.getUUID());
						map.put("channel_id", t.getChannelId());					 
						map.put("business_id", busiArry[i]);
						 
						qzChannelsMapper.insertBatch(map);
					}
				}
			}
			
			
		}	
		
		if(result > 0) {
			return true;
		}else {
			return false;			
		}
	}

	@Override
	public QzChannels getById(String id) {
		return qzChannelsMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<QzChannels> getByMap(Map<String, Object> paramMap) {
		return qzChannelsMapper.selectAll(paramMap);
	}

	@Override
	public boolean updateStatusById(String id) {
		return false;
	}

	@Override
	public boolean delById(String id) {
		QzChannels qz = new QzChannels();
		Map<String, Object> map = new HashMap<String, Object>();
		qz.setChannelId(id);
		qz.setStatus(0);
		qz.setUtime(DateUtils.getNowTime());
		qz.setModifier(LoginUserCache.getUserId());
		map.put("channelId", id);
		List<QzChannelInterface> list = qzchannelinter.selectAll(map);
		System.out.println("接口返回的结果:" + list.size());
		if(list.size() != 0) {
			if(list.get(0).getIsNoDel() == 0) {
				int result = qzChannelsMapper.updateByPrimaryKey(qz);
				if(result >= 1) {
					return true;
				}else {
					return false;			
				}
			}else {
				return false;
			}
		}else {
			int result = qzChannelsMapper.updateByPrimaryKey(qz);
			if(result >= 1) {
				return true;
			}else {
				return false;			
			}
		}
	}

	@Override
	public List<Map<String, Object>> getBybusName(Map<String, Object> map) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String[] param = map.get("name").toString().replace("{", "").replace("}", "").split("},");
//		System.out.println("map的数量:" + param[0] + ";" + param[1]);
		System.out.println("param的数量:" + param.length);
//		if(map.size() > 1) {
		if(map.get("qzname").toString().split(",").length > 1) {
			for(int i=0; i<param.length; i++) {
				Map<String, Object> resmap = new HashMap<String, Object>();
				String[] busname = param[i].replace("{", "").replace("}", "").split(",");
				System.out.println("busname的名称:" + busname[0] + "," + busname[1]);
				for(int j=0; j<busname.length; j++) {
					for(int k=0; k<map.get("qzname").toString().split(",").length; k++) {
						if(map.get("qzname").toString().split(",")[k].equals(busname[j].split("=")[1])) {
							resmap.put("name", map.get("qzname").toString());
							resmap.put("businessId", busname[0].split("=")[1]);
							resmap.put("code", busname[2].split("=")[1]);
							resmap.put("num", "1");
						}else {
							resmap.put("name", busname[1].split("=")[1]);
							resmap.put("businessId", busname[0].split("=")[1]);
							resmap.put("code", busname[2].split("=")[1]);
						}
					}
				}
				list.add(resmap);
			}
		}else {
			for(int i=0; i<param.length; i++) {
				Map<String, Object> resmap = new HashMap<String, Object>();
				String[] busname = param[i].replace("{", "").replace("}", "").split(",");
				for(int j=0; j<busname.length; j++) {
					if(map.get("qzname").equals(busname[j].split("=")[1])) {
						resmap.put("name", map.get("qzname").toString());
						resmap.put("businessId", busname[0].split("=")[1]);
						resmap.put("code", busname[2].split("=")[1]);
						resmap.put("num", "1");
					}else {
						resmap.put("name", busname[1].split("=")[1]);
						resmap.put("businessId", busname[0].split("=")[1]);
						resmap.put("code", busname[2].split("=")[1]);
					}
				}
				list.add(resmap);
			}
		}
//		System.out.println("解析出的数组：" + list.get(0).get("qzname") + "," + list.get(1).get("busname"));
		return list;
	}

	@Override
	public List<QzChannels> getRelateBusi(String channelId) {
		return qzChannelsMapper.getRelateBusi(channelId);
	}
	
	@Override
	public boolean delChannelBusiById(Map<String, Object> map){
		if(qzChannelsMapper.delChannelBusiById(map)>0){
			return true;
		}
		return false;
		
	}

	@Override
	public QzChannels checkName(Map<String, String> paramMap) {		
		return qzChannelsMapper.checkName(paramMap);
	}

	@Override
	public QzChannels checkCode(Map<String, String> paramMap) {
		return qzChannelsMapper.checkCode(paramMap);
	}

}
